<?php echo $header; ?><?php echo $column_left; ?>
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right"><a href="<?php echo $add; ?>" data-toggle="tooltip" title="<?php echo $button_add; ?>" class="btn btn-primary"><i class="fa fa-plus"></i></a>
                <button type="button" data-toggle="tooltip" title="<?php echo $button_delete; ?>" class="btn btn-danger" onclick="confirm('<?php echo $text_confirm; ?>') ? $('#form-information').submit() : false;"><i class="fa fa-trash-o"></i></button>
            </div>
            <h1><?php echo $heading_title; ?></h1>
            <ul class="breadcrumb">
                <?php foreach ($breadcrumbs as $breadcrumb) { ?>
                    <li><a href="<?php echo $breadcrumb['href']; ?>"><?php echo $breadcrumb['text']; ?></a></li>
                <?php } ?>
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        <?php if ($error_warning) { ?>
            <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
        <?php } ?>
        <?php if ($success) { ?>
            <div class="alert alert-success"><i class="fa fa-check-circle"></i> <?php echo $success; ?>
                <button type="button" class="close" data-dismiss="alert">&times;</button>
            </div>
        <?php } ?>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-list"></i> <?php echo $text_list; ?></h3>
            </div>
            <div class="panel-body">
                <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form-information">


                    <!--文章库-->
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab-library">
                            <div class="well">
                                <div class="row">
                                    <div class="col-sm-4">
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <!--<label class="control-label" for="input-name">文章名称</label>-->
                                            <input type="text" name="filter_title"  placeholder="活动名称" id="input-name" class="form-control" value="<?php echo $filter_title; ?>" />
                                        </div>
                                    </div>

                                    <div class="col-sm-4">
                                        <div class="form-group" style="margin-bottom: 0;">
                                            <!--<label class="control-label" for="input-status"> </label>-->
                                            <button type="button" style="display: block;" id="button-filter" class="btn btn-primary"><i class="fa fa-search"></i>筛选</button>

                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="bs-example">
                                <div class="row">
                                    <button style="<?php if($status!='publish') echo 'display:none'; ?>" type="button" id="button-filter" class="btn btn-danger" onclick="show_grade_pop()">群发至年级</button>
                                    <button style="<?php if($status!='publish') echo 'display:none'; ?>" type="button" id="button-filter" class="btn btn-danger" onclick="show_location_pop()">群发至地区</button>
                                </div>
                            </div>

                            <div class="table-responsive">
                                <table class="table table-bordered table-hover">
                                    <thead>
                                    <tr>
                                        <td style="width: 1px;" class="text-center"><input type="checkbox" onclick="$('input[name*=\'selected\']').prop('checked', this.checked);" /></td>
                                        <td class="text-left"><a>标题</a></td>
                                        <td class="text-left"><a>推送时间</a></td>
                                        <td class="text-left"><a>推送状态</a></td>
                                        <td class="text-left"><a>阅读数</a></td>
                                        <td class="text-left"><a>编辑时间</a></td>
                                        <td class="text-left"><a>推送年级/地区</a></td>
                                        <td class="text-right">操作</td>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php if ($data) { ?>
                                        <?php foreach ($data as $item) { ?>
                                            <tr>
                                                <td class="text-center"><?php if (in_array($item[$primary_key_name], $selected)) { ?>
                                                        <input type="checkbox" name="selected[]" value="<?php echo $item[$primary_key_name]; ?>" checked="checked" />
                                                    <?php } else { ?>
                                                        <input type="checkbox" name="selected[]" value="<?php echo $item[$primary_key_name]; ?>" />
                                                    <?php } ?></td>
                                                <td class="text-left"><?php echo $item['post_title']; ?></td>
                                                <td class="text-left"><?php echo $item['last_avail_time']; ?></td>
                                                <td class="text-left"><?php echo isset($item['last_avail_time']) ? "已发送" : ""; ?></td>
                                                <td class="text-right"><?php echo $item['view_count']; ?></td>
                                                <td class="text-left"><?php echo $item['modify_date']; ?></td>
                                                <td class="text-left" style="width: 35%;">
                                                	<span style="display: block;"><?php if(!empty($item['push_grades'])) echo '年级：' . $item['push_grades']; ?></span>
                                                	<span style="display: block;"><?php if(!empty($item['push_cities'])) echo '地区：' . $item['push_cities']; ?></span>
                                                </td>
                                                <td class="text-right">
                                                    <a  class="btn btn-info" href="<?php echo $item['preview']; ?>" data-toggle="tooltip" title="预览" class="btn btn-primary"><i class="fa fa-eye"></i></a>
                                                	<a href="<?php echo $item['edit']; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a>
                                            	</td>
                                            </tr>
                                        <?php } ?>
                                    <?php } else { ?>
                                        <tr>
                                            <td class="text-center" colspan="8"><?php echo $text_no_results; ?></td>
                                        </tr>
                                    <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>


                    </div>

                    <script type="text/javascript" src="view/javascript/bootstrap/js/bootstrap-datetimepicker-master/js/bootstrap-datetimepicker.js"></script>
                    <script type="text/javascript" src="view/javascript/bootstrap/js/bootstrap-datetimepicker-master/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
                    <!--群发文章至年级-->
                    <div class="ui_pop" id="div_grades" style="display: none;">
                        <div class="ui_mask">
                            <div class="ui_header">选择发送群体</div>
                            <div class="ui_item1">
                                <div class="ui_control"><input id="select_all_grades" type="checkbox"  onclick="$('input[name*=\'grades\']').prop('checked', this.checked);" /></div>
                                <div class="control_title" >全选</div>
                            </div>
                            <div class="ui_content">
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="1" type="checkbox" id='cb1'/></div>
                                    <div class="control_title">一年级</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="2" type="checkbox" id='cb2'/></div>
                                    <div class="control_title">二年级</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="3" type="checkbox" id='cb3'/></div>
                                    <div class="control_title">三年级</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="4" type="checkbox" id='cb4'/></div>
                                    <div class="control_title">四年级</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="5" type="checkbox" id='cb5'/></div>
                                    <div class="control_title">五年级</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="6" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">六年级</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="7" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">初一</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="8" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">初二</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="9" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">初三</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="10" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">高一</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="11" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">高二</div>
                                </div>
                                <div class="ui_item">
                                    <div class="ui_control"><input name="grades[]" value="12" type="checkbox" id='cb6'/></div>
                                    <div class="control_title">高三</div>
                                </div>
                            </div>
                            <div class="ui_footer">
                                <div class="slect_date">

                                    <div class="">发送时间</div>
                                    <div class='input-group date' id='datetimepicker1'>
                                        <input type='text'  class="form-control" id="input_sendTime" name="sendTime" />
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                    </div>
                                    <script>
                                        $('#datetimepicker1').datetimepicker({
                                            language: 'zh-CN',
                                            autoclose: true,
                                            todayHighlight: true
                                        });
                                        var rightNow = new Date();
                                        var res = rightNow.toISOString().slice(0,10).replace(/-/g,"");

                                        $('#datetimepicker1').datetimepicker('setStartDate', res);

                                    </script>

                                </div>
                                <div class="btn_set">
                                    <input class="btn btn-primary" type="submit"  form="form-information" name="send" value="确定"></input>
                                    <a class="cancel_btn" onclick="hide_grade_pop()">取消</a>
                                </div>
                            </div>
                        </div>

                    </div>

                    <!--群发文章至地区-->
                    <div id="div_locations" class="ui_pop" ng-app="crm" ng-controller="articleController"  style="display: none;">
                        <div class="ui_mask" style="height: 520px;">
                            <div class="ui_header">选择发送地区</div>
                            <div class="ui_item1" ng-hide="show_cities" ng-cloak>
                                <div class="ui_control"><input id="select_all_province" type="checkbox" ng-click="select_all_province()"  /></div>
                                <div class="control_title" >全选</div>
                            </div>
                            <div class="ui_content" style="height: 395px;" ng-hide="show_cities" ng-cloak>
                                <div class="ui_item" ng-repeat="item in province_list" ng-click="select_province(item)">
                                    <div class="ui_control">{{item.name}}</div>
                                    <div class="control_title"><a class="next_btn"><i class="fa fa-angle-right" aria-hidden="true"></i></a></div>
                                </div>
                            </div>
                            <div class="ui_content" style="height: 330px;" ng-show="show_cities" ng-cloak>
                                <div class="return_btn" ng-click="return_to_province()"> <i class="fa fa-angle-left" aria-hidden="true" ></i>返回选择地区</div>
                                <div class="ui_item2">
                                    <div class="ui_control"><input id="select_all_cities" type="checkbox" ng-click="select_all_city()" /></div>
                                    <div class="control_title" >全选</div>
	                            </div>
                                <div class="ui_item" ng-repeat="item in city_list">
                                    <div class="ui_control"><input name="cities1[]" value="{{item.zone_id}}" type="checkbox" ng-model="item.selected"  /></div>
                                    <div class="control_title">{{item.name}}</div>
                                </div>

                                <div style="display:none" class="ui_item" ng-repeat="item in selected_list">
                                    <div class="ui_control"><input name="cities[]" value="{{item.zone_id}}" type="checkbox" ng-model="item.selected" /></div>
                                </div>
                            </div>
                            <div class="ui_footer" >
                                <div class="slect_date" ng-show="show_cities" ng-cloak>
                                    <div class="">发送时间</div>
                                    <div class='input-group date' id='datetimepicker2'>
                                        <input type='text'  class="form-control" id="input_sendTime_city" name="sendTime_city" />
                                        <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
                                    </div>
                                    <script>
                                        $('#datetimepicker2').datetimepicker({
                                            language: 'zh-CN',
                                            autoclose: true,
                                            todayHighlight: true
                                        });
                                        var rightNow = new Date();
                                        var res = rightNow.toISOString().slice(0,10).replace(/-/g,"");

                                        $('#datetimepicker2').datetimepicker('setStartDate', res);

                                    </script>
                                </div>
                                <div class="btn_set">
                                    <input class="btn btn-primary" type="submit" ng-click="presend()"  form="form-information" name="send_to_city" value="确定" ></input>
                                    <a class="cancel_btn" ng-click="hide_location_pop_angular()">取消</a>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
                <div class="row">
                    <div class="col-sm-6 text-left"><?php echo $pagination; ?></div>
                    <div class="col-sm-6 text-right"><?php echo $results; ?></div>
                </div>
            </div>
        </div>
    </div>
</div>

<!--文章预览-->
<div class="ui_pop" style="display: none;" id="div_preview">
    <div class="article_content">
        <div class="ac_item">
            <a class="ac_close" onclick="$('#div_preview').hide();"><i class="fa fa-times" aria-hidden="true"></i></a>
            <div class="ac_editor">
                <div class="ac_title" id="preview_title"></div>
                <div class="authorinfo">
                    <p id="preview_date"></p>
                </div>
                <div class="ac_details" id="preview_content">
                </div>
            </div>
        </div>
    </div>
</div>


<?php echo $footer; ?>

<script src="view/javascript/angular.min.js"></script>
<script src="view/javascript/angular-route.js"></script>
<script src="view/javascript/angular-resource.min.js"></script>
<script src="view/javascript/app.js"></script>
<script src="view/javascript/controller/articleController.js"></script>
<script type="text/javascript">

//    $("#select_all_grades").click(function(){
//        if ($("#select_all_grades").get(0).checked) {
//            $("input[name='grades[]']").attr("checked","true");
//        }
//        else{
//            $("input[name='grades[]']").removeAttr("checked");
//        }
//
//    })
//    $("#select_all_cities").click(function(){
//        if ($("#select_all_cities").get(0).checked) {
//            $("input[name='cities[]']").attr("checked","true");
//        }
//        else{
//            $("input[name='cities[]']").removeAttr("checked");
//        }
//
//
//    })

    myApp.controller("articleController", function ($scope, $post, $http, $resource, $routeParams) {

        var token = '<?php echo $token; ?>';


        $scope.province_list = [];
        $scope.city_list = [];

        $http({ method: "GET", url: "index.php?route=article/activity/get_province_list_with_city", params:{token:token}}).success(function (result) {

            $scope.province_list = result;

        }).error(function (data, status, headers, config) {
        });

        $scope.show_location = false;
        $scope.show_cities = false;


        $scope.select_province = function (item) {

            $scope.city_list = item.items;
            $scope.show_cities = true;
//            $http({ method: "GET", url: "index.php?route=article/article/get_city_list", params:{token:token,parent_id:item.country_id} }).success(function (result) {
//
//                $scope.city_list = result;
//                $scope.show_cities = true;
//
//            }).error(function (data, status, headers, config) {
//            });

        };

        $scope.selected_list = [];

        $scope.presend = function () {
            for (i = 0; i < $scope.province_list.length; ++i) {
                for (j = 0; j < $scope.province_list[i].items.length; ++j) {
                    if($scope.province_list[i].items[j].selected){
                        $scope.selected_list.push($scope.province_list[i].items[j]);
                    }

                }
            }

        }
        $scope.select_all_province = function () {
            for (i = 0; i < $scope.province_list.length; ++i) {
                for (j = 0; j < $scope.province_list[i].items.length; ++j) {
                    $scope.province_list[i].items[j].selected = $('#select_all_province').is(":checked");;

                }
            }
        }

        $scope.select_all_city = function () {
            for (i = 0; i < $scope.city_list.length; ++i) {
                $scope.city_list[i].selected = $('#select_all_cities').is(":checked");
            }
        }

        $scope.return_to_province = function () {
            $('#select_all_cities').prop('checked', false);
            $scope.show_cities = false;

        };

        $scope.hide_location_pop_angular = function() {
            for (i = 0; i < $scope.province_list.length; ++i) {
                for (j = 0; j < $scope.province_list[i].items.length; ++j) {
                    $scope.province_list[i].items[j].selected = false;

                }
            }
            $('#select_all_province').prop('checked', false);
            $('#select_all_cities').prop('checked', false);
            $('#div_locations').hide();
            $scope.show_cities = false;
        }
    });
</script>

<script type="text/javascript"><!--
    $('#button-filter').on('click', function() {
        var url = 'index.php?route=article/activity&token=<?php echo $token; ?>&status=<?php echo $status; ?>';

        var filter_title = $('input[name=\'filter_title\']').val();

        if (filter_title) {
            url += '&filter_title=' + encodeURIComponent(filter_title);
        }


        location = url;
    });

    function show_grade_pop() {
        $('#div_grades').show();
    }
    function hide_grade_pop() {
        $('#div_grades').hide();
    }


    function send() {
        var grades_array = new Array();
        if ($('#cb1').is(':checked')) grades_array.push("1");
        if ($('#cb2').is(':checked')) grades_array.push("2");
        if ($('#cb3').is(':checked')) grades_array.push("3");
        if ($('#cb4').is(':checked')) grades_array.push("4");
        if ($('#cb5').is(':checked')) grades_array.push("5");
        if ($('#cb6').is(':checked')) grades_array.push("6");

        var grades = grades_array.join();

        $('#div_grades').hide();


        $('input[name=grades]').val(grades);
        $('input[name=sendTime]').val($('#input_sendTime').val());
        $('#posts-filter').submit();
    }
    function show_location_pop() {
        $('#div_locations').show();
    }
    function hide_location_pop() {
        $('#div_locations').hide();
    }
    function preview(id) {
        $('#div_preview').show();

        $.ajax({
            url: 'index.php?route=article/article/preview&token=<?php echo $token; ?>&ID=' +  encodeURIComponent(id),
            dataType: 'json',
            success: function(json) {
                $('#preview_title').html(json.post_title);
                $('#preview_date').text(json.post_modified);
                $('#preview_content').html (json.post_content);
            }
        });
    }
    //--></script>